//
//  HSYClerkManageViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/10.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYClerkManageViewController.h"
#import "HSYNewClertViewController.h"
#import "HSYClerkManageCell.h"
#import "BQActivityView.h"
#import "HSYGetClerkListRequest.h"
#import "HSYClerkListModel.h"

@interface HSYClerkManageViewController ()

@property(nonatomic,strong)HSYClerkListModel *dataModel;

@end

@implementation HSYClerkManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.titleButton setTitle:@"店员管理" forState:UIControlStateNormal];
    [self.rightAreaRBtn setTitle:@"新增店员" forState:UIControlStateNormal];
    
    self.rightAreaRBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    
    [self.rightAreaRBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.rightArea.mas_right).offset(-5);
        make.centerY.mas_equalTo(self.rightArea.mas_centerY);
        make.height.mas_equalTo(@40);
        make.width.mas_equalTo(@76);
    }];
    
    self.tableView.tableFooterView = [UIView new];
    
    [self loadServerData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)hsy_allowPullUp{
    return NO;
}

- (BOOL)hsy_allowPullDown{
    return NO;
}

-(void)handleNavigationAction:(HSYNavigationActionType)actionType{
    if (HSYNavActionRightAreaRight == actionType) {
        [self newClert];
    }
    
    if (HSYNavActionLeftAreaLeft == actionType) {
        [self handleGoBackEvent:nil];
    }
}

-(void)loadServerData{
    HSYGetClerkListRequest *task = [[HSYGetClerkListRequest alloc] init];
    task.parentID = [HSYUserCenter instatce].appAuUser.uid;
    task.sid = [HSYUserCenter instatce].appBizShop.shopID;

    [BQActivityView showActiviTy];
    __weak typeof(self)weakSelf = self;
    
    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        [BQActivityView hideActiviTy];
        
        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            HSYClerkListModel *model = [HSYClerkListModel mj_objectWithKeyValues:response.encryptDataResult];
            weakSelf.dataModel = model;
            
            [weakSelf.tableView reloadData];
        }else{
            [weakSelf showMessage:response.resultMessage];
        }
        
    } failure:^(NSError *error) {
        [BQActivityView hideActiviTy];
        
        if (error.code != NSURLErrorCancelled) {
            [weakSelf showMessage:@"请求超时"];
        }
    }];

}

- (void)newClert{
    HSYNewClertViewController * vc = [[HSYNewClertViewController alloc] init];
    
    __weak typeof(self) weakSelf = self;
    vc.hsyNewClerkSuccessBlock = ^{
        [weakSelf loadServerData];
    };
    
    [self.navigationController pushViewController:vc animated:YES];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataModel.appAuUserList.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 71.0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HSYClerkInfoModel *one = self.dataModel.appAuUserList[indexPath.row];

    HSYClerkManageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HSYClerkManageCell"];
    
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HSYClerkManageCell" owner:nil options:nil] firstObject];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.IdentityCategoryLabel.text = [NSString stringWithFormat:@"  %@  ",one.roleName];
    cell.nameLabel.text = one.realname;
    cell.shopAdressLabel.text = one.shopShortName;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //编辑店员？？？？
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
